// 模块导入
import database from '../../../utils/database'
// 数据库中运营位表的表名
const categoryTableName = 'Category'
// 商品表名
const goodsTableName = 'Product'

// 定义商品类别模型接口
export interface CategoryItem {
    id?: number;            // 标识
    name: string;           // 名称
    description: string;    // 描述
    created_at?: string;    // 创建时间
    sort: number;           // 排序权重
}

// 定义商品模型接口
export interface GoodsItem {
    id?: number;            // 标识
    name: string;           // 名称
    description: string;    // 描述
    created_at?: string;    // 创建时间
    detail: string;         // 详情
    category_id: number;    // 类别
    price: number;          // 价格
    discounted_price: number; // 折扣价格
    stock: number;          // 库存
    image: string;          // 图片
    status: number;         // 状态
    brand: string;          // 品牌
}

// 商品服务类
export class GoodsService {
    // 获取所有类别数据
    getAllCategory() {
        return new Promise((resolve, reject)=>{
            database.queryData(categoryTableName, ['*'], "id IS NOT NULL ORDER BY sort DESC", (data)=>{
                if (!data) {
                    reject("获取类别数据失败")
                } else {
                    let result = data.result
                    resolve(result)
                }
            })
        });
    }
    // 新增类别
    addCategory(ca: CategoryItem) {
        return new Promise((resolve, reject)=>{
            let keys = ['name', 'description', 'sort']
            let values = [ca.name, ca.description, ca.sort]
            database.insertData(categoryTableName, keys, values, (data)=>{
                if (!data) {
                    reject("创建类别失败")
                } else {
                    resolve(null)
                }
            })
        })
    }

    // 更新类别
    updateCategory(ca: CategoryItem) {
        return new Promise((resolve, _reject)=>{
            database.updateData(categoryTableName, `name='${ca.name}', description='${ca.description}', sort=${ca.sort}`, `id = ${ca.id}`)
            resolve(null)
        }) 
    }

    // 新增商品
    addGoods(g: GoodsItem) {
        return new Promise((resolve, reject)=>{
            let keys = ['name', 'description', 'detail', 'category_id', 'price', 'discounted_price', 'stock', 'image', 'status', 'brand']
            let values = [g.name, g.description, g.detail, g.category_id, g.price, g.discounted_price, g.stock, g.image, g.status, g.brand]
            database.insertData(goodsTableName, keys, values, (data)=>{
                if (!data) {
                    reject("创建商品失败")
                } else {
                    resolve(null)
                }
            })
        })
    }

    // 更新商品
    updateGoods(g: GoodsItem) {
        return new Promise((resolve, _reject)=>{
            database.updateData(goodsTableName, `name='${g.name}', description='${g.description}', detail='${g.detail}', category_id=${g.category_id}, stock=${g.stock}, image='${g.image}', status=${g.status}, brand='${g.brand}', price=${g.price}, discounted_price=${g.discounted_price}`, `id = ${g.id}`)
            resolve(null)
        }) 
    }

    // 删除商品
    deleteGoods(id: number) {
        return new Promise((resolve, _reject)=>{
            database.deleteData(goodsTableName, `id = ${id}`)
            resolve(null)
        }) 
    }

    // 获取商品详情
    getGoodsDetail(id: number) {
        return new Promise((resolve, reject)=>{
            database.queryDataFrom(goodsTableName, ['*'], [['id', 'name', 'description', 'sort', 'created_at']], [categoryTableName], ['category_id'], ['category'], `id=${id}`, (data)=>{
                if (!data) {
                    reject("获取商品数据失败")
                } else {
                    let result = data.result
                    resolve(result)
                }
            })
        });
    }

    // 获取商品列表
    getGoodsList(cid: number | undefined = undefined, offset: number, limit: number) {
        return new Promise((resolve, reject)=>{
            let keys = ['id', 'created_at', 'name', 'description', 'category_id', 'price', 'discounted_price', 'stock', 'image', 'status', 'brand']
            let where = ''
            if (cid) {
                where += `category_id=${cid}`
            } else {
                where += 'id IS NOT NULL'
            }
            where += ` limit ${limit} offset ${offset}`
            database.queryDataFrom(goodsTableName, keys, [['id', 'name', 'description', 'sort', 'created_at']], [categoryTableName], ['category_id'], ['category'], where, (data)=>{
                if (!data) {
                    reject("获取商品数据失败")
                } else {
                    let result = data.result
                    resolve(result)
                }
            })
        });
    }
    // 搜索商品
    searchGoods(keyword: string, offset: number, limit: number) {
        return new Promise((resolve, reject)=>{
            // 构建SQL语句
            let keys = ['id', 'created_at', 'name', 'description', 'category_id', 'price', 'discounted_price', 'stock', 'image', 'status', 'brand']
            let where = `(name like '%${keyword}%') OR (description like '%${keyword}%')`
            where += ` limit ${limit} offset ${offset}`
            database.queryDataFrom(goodsTableName, keys, [['id', 'name', 'description', 'sort', 'created_at']], [categoryTableName], ['category_id'], ['category'], where, (data)=>{
                if (!data) {
                    reject("获取商品数据失败")
                } else {
                    let result = data.result
                    resolve(result)
                }
            })
        });
    }
}

// 导出服务类
export default new GoodsService();